package com.zdb.personal.controller;


import com.zdb.personal.po.ZdbCommonCitysPo;
import com.zdb.personal.po.ZdbCommonDistrictPo;
import com.zdb.personal.po.ZdbCommonProvincesPo;
import com.zdb.personal.service.ZdbCommonCitysService;
import com.zdb.personal.service.ZdbCommonDistrictService;
import com.zdb.personal.service.ZdbCommonProvincesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 省份
 */
@RestController
@RequestMapping("/provinces")
public class ZdbCommonProvincesController {
    //省份表
    @Autowired
    private ZdbCommonProvincesService provincesService;

    //城市表
    @Autowired
    private ZdbCommonCitysService commonCitysService;

    //地区表
    @Autowired
    private ZdbCommonDistrictService districtService;

    /**
     * 根据id查询对应的省份
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryByIdProvinces/{id}")
    public ZdbCommonProvincesPo queryByIdProvinces(@PathVariable("id")int id){
        return this.provincesService.getById(id);
    }

    /**
     * 省份 城市  地区 树形菜单数据
     * @return
     */
    @RequestMapping("/queryAllProCityDis")
    public ArrayList<ZdbCommonProvincesPo> queryAllProCityDis(){
        //省份
        List<ZdbCommonProvincesPo> listPro = this.provincesService.list();
//        System.err.println(listPro);

        //城市
        List<ZdbCommonCitysPo> listCitys = this.commonCitysService.list();
//        System.err.println(listCitys);

        //地区
        List<ZdbCommonDistrictPo> listDis = this.districtService.list();
//        System.err.println(listDis);

        //省份
        HashMap<Integer, ZdbCommonProvincesPo> proMap = new HashMap<>();
        for(ZdbCommonProvincesPo s:listPro){
            proMap.put(s.getId(),s);
            //System.err.println(s);
        }
        //城市
        HashMap<Integer, ZdbCommonCitysPo> citysMap = new HashMap<>();
        for(ZdbCommonCitysPo s:listCitys){
            citysMap.put(s.getId(),s);
            //System.err.println(s);
        }
//
        //把地区子节点插入城市父节点
        for(ZdbCommonDistrictPo s:listDis){
            ZdbCommonCitysPo citysPo = citysMap.get(s.getCid());
            if(citysPo.getChildren() == null){citysPo.setChildren(new ArrayList<>());}
                citysPo.getChildren().add(s);
        }
//        System.err.println(listCitys);

        //把城市子节点插入省份父节点
        for(ZdbCommonCitysPo s:listCitys){
            ZdbCommonProvincesPo provinces = proMap.get(s.getPid());
            if(provinces.getChildren() == null){provinces.setChildren(new ArrayList<>());}
            provinces.getChildren().add(s);
        }

        //装入list集合
        ArrayList<ZdbCommonProvincesPo> provinces = new ArrayList<>();
        for(ZdbCommonProvincesPo s:proMap.values()){
            provinces.add(s);
        }
//
//        System.err.println(provinces);
        return provinces;

    }


}
